import {
  ADD_TODO,
  REMOVE_TODO,
  TOGGLE_TODO
} from "./actionType";
import action from './action';
import initialState from './state';
import {
  toRefs
} from "vue";
import {
  useReducer
} from '../../../hooks';

function reducer(state, {
  type,
  payload
}) {
  const {
    onAddTodo,
    onToggleTodo,
    onRemoveTodo
  } = action(state);

  switch (type) {
    case ADD_TODO:
      return onAddTodo(payload);
    case TOGGLE_TODO:
      return onToggleTodo(payload);
    case REMOVE_TODO:
      return onRemoveTodo(payload);
    default:
      break;
  }
}

export default function () {
  const [state, todoDispatch] = useReducer(reducer, initialState);

  return {
    ...toRefs(state),
    todoDispatch
  }
}